<?php //pack11class

class ValuesManager {

    const TEXT_MAX_LENGTH=100;
    const TYPE_DEFAULT="default";
    const TYPE_CITY24="city24";

    public static function getValueByID($id,$type="default") {
        array_push(Tracer::$traceFrom,__CLASS__);
        $field=LanguagesManager::sql();
        if($type==self::TYPE_CITY24) $field="`city24` as `value`";
        $valueData=MySQLGet::process("Select ".$field." from ".MySQL::TABLE_VALUES." where `id`='".$id."' limit 1;");
        //echo MySQLGet::getQuery()."<br/>";
        if(!$valueData) {
            return false;
        }
            if(is_null($valueData[0]["value"])) {
                foreach(LanguagesManager::getLanguages() as $language) {
                        $field=`lang".$language->id."`;
                        if($type==self::TYPE_CITY24) $field="`city24`";
                        $valueData=MySQLGet::process("Select ".$field." as `value` from ".MySQL::TABLE_VALUES." where `id`='{$id}' limit 1");
                        //trace(MySQLGet::getQuery());
                        if($valueData) {
                            if(!is_null($valueData[0]["value"])) {
                                //trace($valueData[0]["value"]." return");
                                //break;
                                return $valueData[0]["value"];
                            } else {
                                //trace("NULL");
                            }
                        } else {
                            //trace("false");
                        }
                }
                //trace("Still not returned");
                return ($type==self::TYPE_CITY24)? NULL:"Label not set";
            } else {
                return $valueData[0]["value"];
            }
    }

    public static function getValuesByProperty(Property $property) {
        $result=array();
        $values=MySQLGet::process("Select `id`, ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `property`='".$property->id."';");
        if(!$values) return $result;
        foreach($values as $row) {
            $value=new Value();
            $value->id=$row["id"];
            $value->label=$row["value"];
            $value->propertyID=$property->id;
            array_push($result, $value);
        }
        return $result;
    }

    public static function createValue(Value $value) {
        $fields=array("`property`");
        $values=array($value->propertyID);
        foreach($value->values as $lang=>$text) {
            $language=LanguagesManager::getLanguage($lang);
            array_push($fields,$language->field);
            array_push($values,"'".$text."'");
        }
        $setValue=MySQLSet::process("Insert into ".MySQL::TABLE_VALUES." (".implode(",", $fields).") values (".implode(",",$values).");");
        if(!$setValue) {
            echo MySQLSet::getQuery();
            return false;
        }

        return MySQLSet::$id;
    }

    public static function cropText($text) {
        array_push(Tracer::$traceFrom,__CLASS__);
        $textData=explode(" ", $text);

        $result="";
        for($i=0; $i<count($textData); $i++) {
            $tempResult=$result." ".$textData[$i];
            if(strlen($tempResult)>self::TEXT_MAX_LENGTH) {
                return $result."...";
            } else {
                $result=$tempResult;
            }
        }
        return $tempResult;
    }

}
?>
